﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;

using System.Web.UI.HtmlControls;

namespace GMS
{
    public partial class Pages_SelectCustomerOwnSink : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                rptrSink.DataSource = Common.GetSinkOptionByCategoryId(SessionVariable.Instance.UserDetails.Options.SinkOptionCategoryId);
                rptrSink.DataBind();
            }
        }
        protected void rptrSink_ItemCommand(object source, RepeaterCommandEventArgs e)
        {

        }
        
        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("SinkOptions.aspx");
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            if (SessionVariable.Instance.UserDetails.IsDataPersisted)
            {
                Response.Redirect("Estimate.aspx");
            }
            else
            {
                int count = rptrSink.Items.Count;

                for (int i = 0; i < count; i++)
                {
                    ImageButton img = (ImageButton)rptrSink.Items[i].FindControl("imgBtn1");
                    DropDownList ddlMount = (DropDownList)rptrSink.Items[i].FindControl("ddlMount");
                    Label lblName = (Label)rptrSink.Items[i].FindControl("lblName");
                    Label lblPrice = (Label)rptrSink.Items[i].FindControl("lblPrice");

                    if (ddlMount != null && lblName != null && lblPrice != null)
                    {
                        char[] trimChars = "Cost: $ ".ToCharArray();
                        string strPrice = lblPrice.Text.TrimStart(trimChars);
                        SessionVariable.Instance.UserDetails.Cost.SinkoutPrice = Convert.ToInt32(strPrice);
                        SessionVariable.Instance.UserDetails.Options.SinkOptionId = Convert.ToInt32(img.ToolTip);

                        if (lblName.Text.ToLower().Contains("undermount"))
                        {
                            SessionVariable.Instance.UserDetails.Options.UnderMountSink = Convert.ToInt32(ddlMount.SelectedValue);
                            SessionVariable.Instance.UserDetails.Cost.Sinkout += SessionVariable.Instance.UserDetails.Options.UnderMountSink;
                        }
                        else
                        {
                            SessionVariable.Instance.UserDetails.Options.DropInSink = Convert.ToInt32(ddlMount.SelectedValue);
                            SessionVariable.Instance.UserDetails.Cost.Sinkout += SessionVariable.Instance.UserDetails.Options.DropInSink;
                        }
                    }
                }
                if (SessionVariable.Instance.UserDetails.Options.SinkOptionId > 0 &&
                    (SessionVariable.Instance.UserDetails.Options.UnderMountSink >= 0 || SessionVariable.Instance.UserDetails.Options.DropInSink >= 0))
                {
                    Response.Redirect("SelectOptions.aspx");
                }
            }
        }
    }
}